﻿using System;
using System.Web;
using System.Web.Security;
using ChapterX.Common;
using ChapterX.Data;
using ChapterX.Data.BooksDSTableAdapters;
using ChapterX.Data.ChaptersDSTableAdapters;

namespace ChapterX.BusinessFacade
{
    public class AccessSystem
    {
        private BooksDS.BookAccessDataTable _bookaccesSchema = null;
        public BooksDS.BookAccessDataTable BookAccessSchema
        {
            get
            {
                if (null == _bookaccesSchema)
                {
                    _bookaccesSchema = new BooksDS.BookAccessDataTable();
                }
                return _bookaccesSchema;
            }
        }

        private ChaptersDS.ChapterAccessDataTable _chapterAccessSchema = null;
        public ChaptersDS.ChapterAccessDataTable ChapterAccessSchema
        {
            get
            {
                if (null == _chapterAccessSchema)
                {
                    _chapterAccessSchema = new ChaptersDS.ChapterAccessDataTable();
                }
                return _chapterAccessSchema;
            }
        }

        public ChaptersDS.ChapterAccessRow GetChapterByChapterId(int chapterId)
        {
            ChapterAccessTableAdapter adapter = new ChapterAccessTableAdapter();
            ChaptersDS.ChapterAccessDataTable table = adapter.GetChapterAccessByChapterId(chapterId);

            if (null != table && table.Rows.Count > 0)
            {
                return table[0];
            }
            else
            {
                return null;
            }
        }


        public BooksDS.BookAccessRow GetBookAccessByAccount(String accessAccount)
        {
            BookAccessTableAdapter adapter = new BookAccessTableAdapter();
            BooksDS.BookAccessDataTable table = adapter.GetBookAccessByAccessAccount(accessAccount);

            if (null != table && table.Rows.Count > 0)
            {
                return table[0];
            }
            else
            {
                return null;
            }
        }

        public BooksDS.BookAccessRow GetBookAccessByBookIdAndCourseId(int bookId, int courseId)
        {
            BookAccessTableAdapter adapter = new BookAccessTableAdapter();
            BooksDS.BookAccessDataTable table = adapter.GetBookAccessByBookIdAndCourseId(courseId, bookId);

            if (null != table && table.Rows.Count > 0)
            {
                return table[0];
            }
            else
            {
                return null;
            }
        }

        public bool Exists(String accessAccount)
        {
            return (null != GetBookAccessByAccount(accessAccount));
        }

        public bool VerifyAccessCode(String accessAccount, String accessPassword)
        {
            BooksDS.BookAccessRow account = GetBookAccessByAccount(accessAccount);

            if (null == account)
            {
                throw new RecordNotExistedException("指定的记录不存在。");
            }
            else
            {
                bool result = account.AccessCode.Equals(accessPassword);

                if (result)
                {
                    //HttpCookie authCookie = new HttpCookie("account", accessAccount);
                    //HttpContext.Current.Response.Cookies.Add(authCookie);

                    HttpContext.Current.Session.Add("account", accessAccount);

                }
                
                return result;
            }
        }

        public void StudentLogin(String accessAccount, String ip)
        {
            try
            {
                using (BookAccessTableAdapter adapter = new BookAccessTableAdapter())
                {
                    //adapter.UserLogin(userName, ip);
                }
            }
            catch
            {
            }
        }

        #region ChapterAccess
        public bool SubmitChapterAccess(ChaptersDS.ChapterAccessRow chapterAccess)
        {
            bool result = false;
            try
            {
                ChapterAccessTableAdapter adapter = new ChapterAccessTableAdapter();

                ChaptersDS.ChapterAccessDataTable data = adapter.GetChapterAccessByChapterId(chapterAccess.ChapterId);

                if (data.Rows.Count == 0)
                {
                    //chapter access信息不存在，直接插入
                    result = InsertChapterAccess(adapter, chapterAccess);
                }
                else
                {
                    //chaper access已存在，更新chapterAcces
                    result = UpdateChapterAccessibility(adapter, chapterAccess);
                }
            }
            catch (Exception e)
            {
                return false;
            }

            return result;
        }

        public bool InsertChapterAccess(ChapterAccessTableAdapter adapter, ChaptersDS.ChapterAccessRow chapterAccess)
        {
            int r = adapter.InsertChapterAccess(
                chapterAccess.ChapterId,
                chapterAccess.CourseId,
                chapterAccess.Accessibility);

            if (r > 0)
                return true;
            else
                return false;
        }

        public bool UpdateChapterAccessibility(ChapterAccessTableAdapter adapter, ChaptersDS.ChapterAccessRow chapterAccess)
        {
            int r = adapter.UpdateChapterAccessibility(
                chapterAccess.ChapterId,
                chapterAccess.CourseId,
                chapterAccess.Accessibility);

            if (r > 0)
                return true;
            else
                return false;
        }

        #endregion

        #region Book Access
        public bool SubmitBookAccess(BooksDS.BookAccessRow bookAccess)
        {
            bool result = false;
            try
            {
                BookAccessTableAdapter adapter = new BookAccessTableAdapter();

                BooksDS.BookAccessDataTable data = adapter.GetBookAccessByBookIdAndCourseId(bookAccess.CourseId, bookAccess.BookId);

                if (data.Rows.Count == 0)
                {
                    //book access信息不存在，直接插入
                    result = InsertBookAccess(adapter, bookAccess);
                }
                else
                {
                    //book access已存在，更新chapterAcces
                    result = UpdateBookAccess(adapter, bookAccess);
                }
            }
            catch (Exception e)
            {
                return false;
            }

            return result;
        }

        public bool InsertBookAccess(BookAccessTableAdapter adapter, BooksDS.BookAccessRow bookAccess)
        {
            int r = adapter.InsertBookAccess(
                bookAccess.CourseId,
                bookAccess.BookId,
                bookAccess.AccessAccount,
                bookAccess.AccessCode);

            if (r > 0)
                return true;
            else
                return false;
        }

        public bool UpdateBookAccess(BookAccessTableAdapter adapter, BooksDS.BookAccessRow bookAccess)
        {
            int r = adapter.UpdateBookAccessAccountAndCode(
                 bookAccess.BookId,
                 bookAccess.CourseId,
                 bookAccess.AccessAccount,
                 bookAccess.AccessCode);

            if (r > 0)
                return true;
            else
                return false;

        }
        #endregion
    }
}